﻿using System;
using System.Linq;
using System.Windows.Forms;
using UniMana.Data;
using UniMana.Utils.Data;

using UniMana.Utils.Configs;
using System.Data;

namespace UniMana.Win.Personnel
{
    public partial class FrmAttendance_SubCoppy : DevExpress.XtraEditors.XtraForm
    {
        public int iOption = 0;
        public string symbol = string.Empty;
        private DBMain m_data = new DBMain();
        const int I_DAY = 1;
        const int I_PERIOD = 2;
        bool validate = true;
        int month = 0;
        int Year = 2013;
        public int iFrom = 0;
        public int iTo = 0;

        public FrmAttendance_SubCoppy(int month, int year)
        {
            InitializeComponent();
            this.LoadData();
            DateTime dNow = m_data.m_DCSave.GetSystemDate();
            dteFrom.EditValue = dNow.Date;
            dteTo.EditValue = dNow.Date;
            this.month = month;
            this.Year = year;

            for (int i = 26; i <= DateTime.DaysInMonth(this.Year, this.month); i++)
            {
                dteFrom.Properties.Items.Add(i);
            }


            for (int i = 1; i <= 25;i++ )
            {
                dteTo.Properties.Items.Add(i);
            }
            dteFrom.SelectedIndex = 0;
            dteTo.SelectedIndex = 0;
        }

        private void SubFrmCoppy_Load(object sender, EventArgs e)
        {

        }
        public void LoadData()
        {
            var symbolList = from li in m_data.m_DCReadOnly.Catalogue_Idles
                             where li.V2 == true
                             select new
                             {
                                 li.Catalogue_IdleKey,
                                 li.ID,
                                 li.Name,
                                 Checked = false
                             };
            gctSymbol.DataSource = symbolList.ToDataTable();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            DataTable tbl = (gctSymbol.DataSource as DataTable);
            int i = 0;
            foreach (DataRow r in tbl.Rows)
            {

                if (r["Checked"].ToString() == "True")
                {
                    i++;
                    if(i==2) return;
                }
            }

            if (validate)
            {
                this.iOption = I_PERIOD;
                iFrom = int.Parse(dteFrom.Text);
                iTo = int.Parse(dteTo.Text);

                foreach (DataRow r in tbl.Rows)
                {
                    if (r["Checked"].ToString() == "True")
                    {
                        this.symbol = r["ID"].ToString();
                        this.Close();
                        return;
                    }
                }

                MsgConfig.ShowErrorDialog("Chưa chọn ký hiệu chấm công");
            }
            
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            this.Close();
            iOption = -1;
        }

        private void dteTo_EditValueChanged(object sender, EventArgs e)
        {
            
            
            if (dteTo.EditValue.ToDateTime().Month > m_data.m_DCReadOnly.GetSystemDate().Month
                || (dteTo.EditValue.ToDateTime().Month == m_data.m_DCReadOnly.GetSystemDate().Month)
                    && dteTo.EditValue.ToDateTime().Day>25)
            {
                MsgConfig.ShowErrorDialog("Tháng không hợp lệ, vui lòng kiểm tra lại");
                validate = false;
            }
        }
    }
}